package com.yh.yh_06single_activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    RadioButton r1 = null;
    RadioButton r2 = null;
    RadioButton r3 = null;
    RadioButton r4 = null;
    RadioGroup radioGroup = null;
   // RadioButton currentRadioButton=null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        r1 = (RadioButton) findViewById(R.id.a);
        r2 = (RadioButton) findViewById(R.id.b);
        r3 = (RadioButton) findViewById(R.id.c);
        r4 = (RadioButton) findViewById(R.id.d);
        r1.setClickable(true);
        //radioGroup.setOnCheckedChangeListener(mChangeRadio);
        Button btn1_sure =(Button)findViewById(R.id.sure);
        Button btn2_cancel =(Button)findViewById(R.id.cancel);
        btn1_sure.setOnClickListener(new btn1_sure());
        btn2_cancel.setOnClickListener(new btn2_cancel());
    }

    class btn1_sure implements OnClickListener{
        @Override
        public void onClick(View v) {
            String ans ="";
            if(r1.isChecked()){
                ans = "小猪佩佩";
            }
            else if (r2.isChecked()) {

                ans = "小猪佩奇";
            }
            else if (r3.isChecked()) {

                ans = "朱咪咪";
            }
            else if (r4.isChecked()) {

                ans = "发羊癫";
            }
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, otherActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("ans", ans);
            intent.putExtras(bundle);
            MainActivity.this.startActivity(intent);
        }

    }
    class  btn2_cancel implements OnClickListener{
        @Override
        public void onClick(View v) {
            radioGroup.clearCheck() ;
            setTitle("com.yh.yh_06single_activity");
        }

    }
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                Bundle bunde = data.getExtras();
                String ans = bunde.getString("ans");
                break;
            default:
                break;
        }
    }
}